Skip to content

Translate primitive collections projected through set operations - #39561

Open
ajcvickers wants to merge 6 commits into
dotnet:mainfrom
ajcvickers:fix/33258-set-operation-primitive-collection
Open

ajcvickers wants to merge 6 commits into
dotnet:mainfrom
ajcvickers:fix/33258-set-operation-primitive-collection

Conversation

@ajcvickers

Copy link
Copy Markdown
Contributor

Fixes #33258

After a set operation (Concat, Union, etc.) over primitive collection columns, the collection is projected out of a subquery and the PrimitiveCollectionReference that kept it queryable is lost. The AsQueryable added during normalization was then stripped, leaving an untranslatable Enumerable.Contains/Count/etc.

This is a symptom of the nav-expansion limitations tracked in #32957, but it can be fixed locally:

  • Nav expansion: preserve the AsQueryable when its argument is a primitive collection (array or IList<T> of non-entity elements, excluding byte[]) that was projected out of a subquery, directly or as a member of an anonymous type.
  • Relational translator: when AsQueryable can't otherwise be translated and its argument translates to a column with an element type mapping, translate the column as a primitive collection.

Tests

New spec tests (Concat, Union, Distinct, anonymous types, string collections, parameter collections, Where/Count, Select/Count) with overrides for Sqlite, SQL Server (including JSON type and old compatibility levels) and Cosmos. SQL Server json isn't comparable, so Union/Distinct expect a SqlException there. Without the fix, 8 of the 9 tests I tried fail.

Ran test.cmd: all assemblies pass except two ManyToMany tests (SelectMany_with_collection_selector_having_subquery, Many_to_many_load_works_when_join_entity_has_custom_key), which are flaky and also fail without this change.

Notes

  • Some shapes that previously fell back to client evaluation after a set operation now translate to the server, which can change e.g. SQL Server collation or Sum overflow behavior.
  • The fallback calls TranslatePrimitiveCollection with property: null; providers that dereference property (e.g. Npgsql) should be checked.
  • Nested collections and complex-type element collections after a set operation aren't covered by tests.

🤖 Generated with Claude Code

@ajcvickers
ajcvickers requested a review from a team as a code owner November 7, 2026 17:00
Copilot AI balanced review requested due to automatic review settings November 7, 2026 17:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 8, 2026 10:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The JSON-type tests can pass on unintended SQL failures without expected command baselines, and the new fallback lacks precompiled-query regression coverage for Contains/Count over projected collections.

2 open issues

Review effort: Balanced

@ajcvickers
ajcvickers force-pushed the fix/33258-set-operation-primitive-collection branch from 03017c4 to 807deb8 Compare November 14, 2026 11:49
Copilot AI balanced review requested due to automatic review settings November 14, 2026 11:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 15, 2026 13:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Projected collections can return incorrect results with mixed element mappings or SQL Server’s converted-element JSON_CONTAINS path, while also risking provider compatibility and client evaluation.

5 open issues
2 resolved since last review

Review effort: Balanced

@ajcvickers
ajcvickers force-pushed the fix/33258-set-operation-primitive-collection branch from db1e1f1 to 370386a Compare November 24, 2026 14:30
Copilot AI balanced review requested due to automatic review settings November 24, 2026 14:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@ajcvickers
ajcvickers force-pushed the fix/33258-set-operation-primitive-collection branch from 370386a to 232257f Compare November 25, 2026 13:40
Copilot AI balanced review requested due to automatic review settings November 25, 2026 13:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@ajcvickers
ajcvickers force-pushed the fix/33258-set-operation-primitive-collection branch from 232257f to dbe8d62 Compare November 25, 2026 14:16
Copilot AI balanced review requested due to automatic review settings November 25, 2026 14:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 25, 2026 17:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The compatibility check ignores differing value converters, allowing collections with matching store and provider types to concatenate with incorrect filtering and materialization.

1 open issue
5 resolved since last review
1 file not reviewed
  • src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Generated file

Review effort: Balanced

Comment on lines +2352 to +2354
&& (elementTypeMapping1.StoreType != elementTypeMapping2.StoreType
|| (elementTypeMapping1.Converter?.ProviderClrType ?? elementTypeMapping1.ClrType)
!= (elementTypeMapping2.Converter?.ProviderClrType ?? elementTypeMapping2.ClrType)))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a case of #10434. We should not attempt to fix this here.

ajcvickers and others added 6 commits November 26, 2026 10:29
Fixes dotnet#33258

After a set operation (Concat, Union, etc.) over primitive collection columns, the
collection is projected out of a subquery and the PrimitiveCollectionReference that
kept it queryable is lost. The AsQueryable added during normalization was then stripped,
leaving an untranslatable Enumerable.Contains/Count/etc.

Preserve the AsQueryable in navigation expansion for such projected primitive
collections, and translate the column as a collection in the relational translator.

Adds spec tests with overrides for Sqlite, SQL Server (including JSON type and old
compatibility levels) and Cosmos.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The new tests asserted SQL Server 2025 baselines (JSON_CONTAINS, and a
SqlException for UNION/DISTINCT over the json type) unconditionally. On
earlier versions the column is nvarchar(max), so the OPENJSON translation
is used and UNION/DISTINCT succeed. Branch on IsJsonTypeSupported.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…n collections

The json data type can't be used with UNION or DISTINCT, so these tests expect a SqlException. An empty AssertSql() baseline excludes failed commands, so any SqlException passed. List the expected failed statements so the translation is verified.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…tions

Covers Contains with a captured value and Count over a primitive collection column projected out of a Concat, so interceptor generation, compilation and execution are validated for this shape.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
- Throw when a set operation combines primitive collection columns whose element
  mappings aren't compatible (e.g. one with a value converter, one without), instead of
  silently reading and querying both with the first operand's mapping.
- Don't use SQL Server JSON_CONTAINS when the collection's elements are stored in a
  different representation than the item (e.g. int converted to string).
- Document that TranslatePrimitiveCollection can also receive a projected collection column.
- Add regression tests for mixed and shared element conversions, List<T> collections and
  a client-evaluated Contains argument after a set operation.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…19/2022

Concat_of_column_collections_then_Select_Contains_with_client_argument and
Concat_of_List_column_collections_then_Contains asserted the JSON_CONTAINS
translation, which is only used when the json type is supported (SQL
Server 2025). Branch on IsJsonTypeSupported and add the OPENJSON baselines
for earlier versions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ajcvickers
ajcvickers force-pushed the fix/33258-set-operation-primitive-collection branch from c2a9856 to 928220f Compare November 26, 2026 10:32
Copilot AI balanced review requested due to automatic review settings November 26, 2026 10:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EF Core 8 Query Translation Bug: Array operations with union

2 participants